#include "Hologram.h"
#include "GamePlayState.h"
#include "Camera.h"
#include "Events\Event.h"

ControlScheme Hologram::scheme = WASD_JKL;

Hologram::Hologram(float range) : GameObject()
{
	this->objectID = "Hologram";
	this->player = GamePlayState::GetInstance()->GetPlayer();

	this->showOnKill = false;
	this->playerKilled = false;
	this->range = range;
	this->distanceToPlayer = 9999999.0f;
	this->inRangeOfPlayer = false;
	this->alpha = 0.0f;
}

void Hologram::SetControlScheme(ControlScheme scheme)
{
	Hologram::scheme = scheme;
}

void Hologram::AddImage(int id)
{
	images.push_back(id);
}

void Hologram::Update(float deltaTime)
{
	if(showOnKill == true && playerKilled == false)
		return;

	this->distanceToPlayer = (float)abs(((player->GetHitBox().right + player->GetHitBox().left) >> 1) - ((hitBox.right + hitBox.left) >> 1));
	this->inRangeOfPlayer = distanceToPlayer < range;

	int sign = inRangeOfPlayer ? +1 : -1;

	this->alpha += deltaTime * 500.0f * sign;

	if(alpha > 255.0f)
		alpha = 255.0f;
	else if(alpha < 0.0f)
		alpha = 0.0f;
}

void Hologram::Render(void) const
{
	CSGD_TextureManager::GetInstance()->Draw(images[(int)scheme], 
		(int)(GetPosX() - Camera::GetInstance()->OffsetX()),
		(int)(GetPosY() - Camera::GetInstance()->OffsetY()),
		1.0f, 1.0f, nullptr, 0.0f, 0.0f, 0.0f, D3DCOLOR_ARGB(max((int)alpha - (rand() % 20), 0), 255, 255, 255));
}

void Hologram::HandleEvent(Event* e)
{
	vector<string>* tags = getSenderTags(e->GetEventID());

	for(unsigned int i = 0; i < tags->size(); ++i)
	{
		if(((GameObject*)e->GetSender())->GetTag() == (*tags)[i])
		{
			if(e->GetEventID() == "KillPlayer")
			{
				playerKilled = true;
				break;
			}
		}
	}
}